package com.ruoyi.system.controller;

import cn.hutool.core.lang.Snowflake;
import cn.hutool.core.lang.generator.SnowflakeGenerator;
import com.alibaba.fastjson.JSON;
import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.core.web.controller.BaseController;
import com.ruoyi.system.domain.Distract;
import com.ruoyi.system.domain.Users;
import com.ruoyi.system.service.DistractService;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.concurrent.TimeUnit;

@RestController
@RequestMapping("distract")
public class DistractController extends BaseController {

    @Autowired
    DistractService distractService;

    @Autowired
    RedisTemplate redisTemplate;
    @Autowired
    RabbitTemplate rabbitTemplate;

    @PostMapping("addDistract")
    public int addDistract(@RequestBody Distract distract){
        Snowflake snowflake = new Snowflake(1, 1);
        distract.setId(snowflake.nextIdStr());
        distract.setHouseId(snowflake.nextIdStr());
        Boolean b = redisTemplate.opsForValue().setIfAbsent(distract.toString(), "addDistract", 5, TimeUnit.MINUTES);
        int j = 0;
        if(b){
            int i = distractService.addHouse(distract);
            int x = distractService.addHouseStyle(distract);

            String[] ids = distractService.selectRoomId(distract.getHouseId());
            for (String id : ids) {
                int n = distractService.addRoomUrl(distract.getFrontCover(),id);
            }
            j= distractService.addHouseUrl(distract);
            rabbitTemplate.convertAndSend("PublicExchange", "", JSON.toJSONString(distract));

        }
        return j;
    }

    @GetMapping("getPublicFacilityOptions")
    public R getPublicFacilityOptions(){
       List<Users> list =  distractService.getPublicFacilityOptions();
       return R.ok(list);
    }

    @GetMapping("getPrivateFacilityOptions")
    public R getPrivateFacilityOptions(){
        List<Users> list =  distractService.getPrivateFacilityOptions();
        return R.ok(list);
    }

    @GetMapping("getManagerOptions")
    public R getManagerOptions(){
        List<Users> list = distractService.getManagerOptions();
        return R.ok(list);
    }

}
